package com.weatherquery.servlet;

import com.weatherquery.informbean.WeatherData;
import com.weatherquery.service.WeatherService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author Natural-Pride
 * @Date 2023/12/17 21:32
 * @注释 此段代码是一个 Java Web应用中的一个 Servlet类,其作用是获取天气信息并转发到 JSP页面
 */
@WebServlet("/weatherListServlet")
public class WeatherListServlet extends HttpServlet{
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        doPost(request, response);
    }
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //创建一个 WeatherService对象
        WeatherService weatherService =new WeatherService();
        //调用 WeatherService的 listWeather方法,获取天气信息,并将返回的 List保存在 weatherList变量中
        List<WeatherData> weatherList = weatherService.listWeather();
        //将 weatherList保存在 request中
        request.setAttribute("weatherList", weatherList);
        //转发请求到 manage.jsp页面
        request.getRequestDispatcher("manage.jsp").forward(request, response);
    }
}

